I modified my Mausberry script to change the display on my TFT to show that the Pi is shutting down. That said, I've seen other python implementations to try to do the same thing. Most seemed to be less "foolproof" than the Mausberry method, so I didn't use python to manage the shutdown. I'm still quite a hack at this, I'm still cleaning things up, and I'm sure my code is messy, but this is my modified switch.sh
#!/bin/bash
#this is the GPIO pin connected to the lead on switch labeled OUT
GPIOpin1=18
#this is the GPIO pin connected to the lead on switch labeled IN
GPIOpin2=17
echo "$GPIOpin1" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio$GPIOpin1/direction
echo "$GPIOpin2" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio$GPIOpin2/direction
echo "1" > /sys/class/gpio/gpio$GPIOpin2/value
while [ 1 = 1 ]; do
power=$(cat /sys/class/gpio/gpio$GPIOpin1/value)
if [ $power = 0 ]; then
sleep 1
else
#load image
sudo python /home/pi/Adafruit_Python_ILI9341/examples/imageparam2.py "/home/pi/Adafruit_Python_ILI9341/examples/powering-off.png"
sudo poweroff
fi
done
The Mausberry script is pretty inefficient if you want to monitor buttons as it's constantly polling. From my research the "wait for edge" method is less processor intensive. This is my "reset button" python script that controls the backlight on my secondary TFT. My code may be messy, but I noticed less CPU load when using this method compared to the Mausberry method.
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
# set up BCM GPIO numbering
GPIO.setmode(GPIO.BCM)
# turn off warnings
GPIO.setwarnings(False)
# tell the script that we're going to output data on GPIO 23
GPIO.setup(23,GPIO.OUT)
# tell the script we're going to use GPIO 27 for input and act as a pull up resistor
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# the default state of the light is on
light = 1
# run this unless there's an exception
try:
# Turn ON Backlight by default
GPIO.output(23,GPIO.HIGH)
# do this forever
while True:
# use interrupt to wait for button to be pressed
GPIO.wait_for_edge(27, GPIO.FALLING)
# if the light is on, turn the light off
if light == 1:
#Turn OFF Backlight
GPIO.output(23,GPIO.LOW)
#change the state of the light variable so the script knows the light is off
light = 0
# if the light is off, turn the light on
elif light == 0:
# Turn ON Backlight
GPIO.output(23,GPIO.HIGH)
#change the state of the light variable so the script knows the light is on
light = 1
# wait a little while to account for bounce in the button
time.sleep(0.2)
# keep running unless you Ctrl+C to break out of it
except KeyboardInterrupt:
# reset the GPIO pins used in this program
GPIO.cleanup()